#include "stdafx.h"
#include "IndexesToSearch.h"
#include "IndexMap.h"
#include <algorithm>
#include <string>
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <fstream>
#include <map>
#include <sstream>

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/archive_exception.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/set.hpp>

bool greaterForStringToInt (const std::pair<std::string,int>& i, const std::pair<std::string,int>& j) 
{
	return (i.first<j.first); 
}

bool greaterForStringToVectorInt (const std::pair<std::string, std::vector<int>>& i, 
								  const std::pair<std::string, std::vector<int>>& j) 
{
	return (i.first < j.first); 
}

IndexesToSearch::IndexesToSearch()
{

}

IndexesToSearch::~IndexesToSearch()
{

}

bool IndexesToSearch::containsWord(std::string& word, std::string& filename)
{
	using namespace std;

	

	vector<pair<string,int>>::iterator itIndex =
		lower_bound(wordToIndex.begin(), wordToIndex.end(), pair<string, int>(word, 0), greaterForStringToInt);

	if (itIndex == wordToIndex.end())
	{
		return false;
	}

	vector<pair<string,vector<int>>>::iterator itFile =
		lower_bound(filenameToIndex.begin(), filenameToIndex.end(),
		pair<string,vector<int>>(filename,vector<int>()), greaterForStringToVectorInt); //TODO: 

	vector<int>& indexes = itFile->second;

	vector<int>::iterator itIndexInFile =
		lower_bound(indexes.begin(), indexes.end(),itIndex->second);

	return (itIndexInFile!=indexes.end());
}

std::set<std::string>* IndexesToSearch::containsWords(std::set<std::string>& words,std::string& filename)
{
	using namespace std;

	std::set<std::string>* findedWord = new std::set<std::string>();

	vector<pair<string,vector<int>>>::iterator itFile =
		lower_bound(filenameToIndex.begin(), filenameToIndex.end(),
		pair<string,vector<int>>(filename,vector<int>()), greaterForStringToVectorInt); 

	for (std::set<std::string>::iterator word = words.begin(); word!=words.end(); word++)
	{
		vector<pair<string,int>>::iterator itIndex =
		lower_bound(wordToIndex.begin(), wordToIndex.end(), pair<string,int>(*word,0), greaterForStringToInt);

		if ((itIndex!=wordToIndex.end())&&(itIndex->first == (*word)))
		{
			vector<int>::iterator itIndexInFile =
				lower_bound(itFile->second.begin(), itFile->second.end(),itIndex->second);

			if ((itIndexInFile!=itFile->second.end())&&((*itIndexInFile)==itIndex->second))
			{
				findedWord->insert(*word);
			}
		}

	}

	return findedWord;
}

bool IndexesToSearch::containsIndex(std::string& filename) const
{
	using namespace std;

	vector<pair<string,vector<int>>>::const_iterator itFile =
		lower_bound(filenameToIndex.begin(), filenameToIndex.end(),
		pair<string,vector<int>>(filename,vector<int>()), greaterForStringToVectorInt);

	return filename==itFile->first;
}